package Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Util.ExportSqlUtil;

public class ExportSQLServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String path = "pages/ExportSqlView.jsp";
        String method = request.getParameter("method");
        if (method.equals("exp")) {
            System.out.println("Servlet->数据开始备份");
            if (ExportSqlUtil.backup("root", "950906", "station")) {
                request.setAttribute("info_exp", "数据备份已完成");
            } else {
                request.setAttribute("info_exp", "数据备份失败");
            }
        } else if (method.equals("tru")) {
            //清空数据库
            if (ExportSqlUtil.truncate_table()) {
                request.setAttribute("info_tru", "数据库已成功还原");
            } else {
                request.setAttribute("info_tru", "数据库还原失败");
            }
        }
        request.getRequestDispatcher(path).forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
